package com.peas.platform.common.core.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.peas.platform.common.core.constant.Constants;
import com.peas.platform.common.core.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author caizhh
 */
@Slf4j
public class ResponseUtil {

    private static final String keyCode = "code";
    private static final String keyMessage = "message";
    private static final String keyData = "data";

    public static <T> List<T> getObjectListFromResponse(Class<T> clazz, JSONObject o, String errorMsg) {
        Assert.notNull(o, errorMsg);
        String msg = StrUtil.isBlank(errorMsg) ? "获取数据为空" : errorMsg;
        if (!Constants.SUCCESS_CODE.equals(o.getString(keyCode))) {
            throw new BaseException(o.getString(keyCode), o.getString(keyMessage));
        }
        JSONArray arr = o.getJSONArray(keyData);
        if (CollUtil.isEmpty(arr)) {
            return new ArrayList<>();
        }
        return arr.toJavaList(clazz);
    }

    public static <T> List<T> getObjectListFromResponse(Class<T> clazz, JSONObject o) {
        return getObjectListFromResponse(clazz, o, "获取数据失败");
    }

    public static <T> T getObjectFromResponse(Class<T> clazz, JSONObject o, String errorMsg) {
        Assert.notNull(o, errorMsg);
        String msg = StrUtil.isBlank(errorMsg) ? "获取数据为空" : errorMsg;
        if (!Constants.SUCCESS_CODE.equals(o.getString(keyCode))) {
            throw new BaseException(o.getString(keyCode), o.getString(keyMessage));
        }
        return o.getObject(keyData, clazz);
    }

    public static <T> T getObjectFromResponse(Class<T> clazz, JSONObject o) {
        return getObjectFromResponse(clazz, o, "获取数据失败");
    }

    public static JSONObject getJSONObjectFromResponse(JSONObject o, String errorMsg) {
        Assert.notNull(o, errorMsg);
        String msg = StrUtil.isBlank(errorMsg) ? "获取数据为空" : errorMsg;
        if (!Constants.SUCCESS_CODE.equals(o.getString(keyCode))) {
            throw new BaseException(o.getString(keyCode), o.getString(keyMessage));
        }
        return o.getJSONObject(keyData);
    }

    public static JSONObject getJSONObjectFromResponse(JSONObject o) {
        return getJSONObjectFromResponse(o, "获取数据失败");
    }

    public static JSONArray getJSONArrayFromResponse(JSONObject o, String errorMsg) {
        Assert.notNull(o, errorMsg);
        String msg = StrUtil.isBlank(errorMsg) ? "获取数据为空" : errorMsg;
        if (!Constants.SUCCESS_CODE.equals(o.getString(keyCode))) {
            throw new BaseException(o.getString(keyCode), o.getString(keyMessage));
        }
        return o.getJSONArray(keyData);
    }

    public static JSONArray getJSONArrayFromResponse(JSONObject o) {
        return getJSONArrayFromResponse(o, "获取数据失败");
    }

    public static void print(HttpServletResponse response) {
        log.info("【response】status:" + response.getStatus());
        log.info("【response】characterEncoding:" + response.getCharacterEncoding());
        log.info("【response】contentType:" + response.getContentType());
        Map<String, String> header = new HashMap<>();
        Collection<String> headers = response.getHeaderNames();
        if (CollUtil.isNotEmpty(headers)) {
            for (String name : headers) {
                header.put(name, response.getHeader(name));
            }
        }
        log.info("【response】header:" + JSONUtil.toJsonStr(header));
    }

}
